﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class contactUser : AbstractPage {
	public static string REQUEST_TO_ID = "toId";

	protected void Page_Load(object sender, EventArgs e) {
		//Look up info on user we're sending mail to
		User user = this.GetUser();
		this.lblTo.Text = user.Email + "(" + user.LastName + ", " + user.FirstName + " " + user.MiddleName + ")";
	}

	protected void btnSend_Click(object sender, EventArgs e) {	
		IScheduleService scheduleService = new ScheduleServiceImpl();
		User fromUser = (User) this.Session[AbstractPage.SESSION_USER];

		//Look up info on user we're sending mail to
		User toUser = this.GetUser();
		if (toUser.Email == null) {
			this.addMessage(new Message(Message.Type.ERROR, "The user you selected was not found in the system"));
		} else {
			scheduleService.contactUser(fromUser, toUser, this.txtbxMessage.Text);
			this.addMessage(new Message(Message.Type.INFO, "Mail sent to " + toUser.Email + "!"));
			this.Server.Transfer("mainMenu.aspx", true);
		}
	}

	protected User GetUser() {
		//Look up info on user we're sending mail to
		IProfileService profileService = new ProfileServiceImpl();
		UserSearchCriteria userCriteria = new UserSearchCriteria();
		User toUser = new User();
		toUser.Id = int.Parse(this.Request[contactUser.REQUEST_TO_ID]);
		userCriteria.Ids.Add(toUser.Id, toUser);
		profileService.findAccounts(userCriteria);
		return toUser;
	}

	protected void btnCancel_Click(object sender, EventArgs e) {
		Server.Transfer("mainMenu.aspx");
	}
}
